import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/user_friend/user_friend_reward_list_model.dart';
import 'package:yysk_app/net/api/user_api.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/pages/nft_pages/search/search_page.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/widget/custom_app_bar2.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/refresh_and_scroll_widget.dart';
import 'package:yysk_app/widget/search_input_widget.dart';

class MyFriendRewardPage extends StatefulWidget {
  static const path = '/MyFriendRewardPage';
  const MyFriendRewardPage({super.key});
  @override
  State<StatefulWidget> createState() => _MyFriendRewardPage();
}

class _MyFriendRewardPage extends State<MyFriendRewardPage> {
  final TextEditingController _searchController = TextEditingController();
  final ScrollController scrollController = ScrollController();
  int pageNo = 1;
  int pageSize = 10;
  late RxString labelNameReward = '好友奖励'.obs;
  final showClose = false.obs;
  final friendRewardList = RxList<UserFriendRewardItemModel>();
  var level = '1'.obs;

  @override
  void initState() {
    super.initState();
    level.value = Get.parameters['level'] ?? Get.arguments['level'];
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        _getPageRewardList(loadMore: true);
      }
    });
    _getPageRewardList();
  }

  @override
  void dispose() {
    super.dispose();
    scrollController.dispose();
  }

  _getPageRewardList({loadMore = false}) {
    if (loadMore) {
      pageNo++;
    } else {
      pageNo = 1;
    }

    try {
      http.netFetch<UserFriendRewardListModel>(
        UserApi.getPageReward,
        params: {'pageNo': pageNo, 'pageSize': 20, 'level': level.value},
        success: (result) {
          List<UserFriendRewardItemModel> items = result.data?.list ?? [];
          if (loadMore && items.isEmpty) {
            pageNo--;
          }
          if (!loadMore) {
            friendRewardList.clear();
          }
          setState(() {
            friendRewardList.addAll(items);
          });
        },
      );
    } catch (e) {
      if (loadMore) {
        pageNo--;
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    double top = MediaQuery.of(context).padding.top;
    return Scaffold(
      backgroundColor: AppColors.backgroundColorBlack,
      body: Obx(() {
        return Stack(
          children: [
               Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: ImageAssetWidget(
              width: 375.px,
              height: 812.px,
              imageUrl: 'static/images/bg.png',
            ),
          ),

               Container(
          padding: EdgeInsets.fromLTRB(0, top, 0, 0),
          child: Column(
            children: [
              CustomAppBar2(
                title: labelNameReward.value,
                context: context,
                textColors: AppColors.fontColor,
              ),
              GestureDetector(
                onTap: () {
                  Get.toNamed(
                    '${SearchPage.path}?type=reward&level=${level.value}',
                    arguments: {'type': 'reward', 'level': level.value},
                  );
                },
                child: Container(
                  padding: EdgeInsets.only(left: 14.px, right: 14.px),
                  child: SearchInputWidget(
                    hint: '输入用户ID',
                    controller: _searchController,
                    showClose: showClose.value,
                    disabled: true,
                    onChanged: (value) {},
                    onSearch: (value) {},
                    top: top,
                  ),
                ),
              ),
              hiSpace(height: 31.px),
              Expanded(
                child: Column(
                  children: [
                    Expanded(
                      child: Container(
                        padding: EdgeInsets.fromLTRB(12.px, 0.px, 12.px, 0.px),
                        child: RefreshAndScrollWidget(
                          onRefresh: _handleRefresh,
                          scrollController: scrollController,
                          itemBuilder: (item, index) => _itemWidget(item),
                          itemCount: friendRewardList.length,
                          items: friendRewardList,
                          emptyText: '暂无数据~',
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        )
     
          ],
        );
      }),
    );
  }

  Future<void> _handleRefresh() async {
    _getPageRewardList();
    return;
  }

  _itemWidget(item) {
    return Container(
      padding: EdgeInsets.all(10.px),
      margin: EdgeInsets.only(bottom: 8.px),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.px),
        border: Border.all(color: AppColors.backgroundColor.withOpacity(0.1)),
        color: AppColors.backgroundColorBlackLight,
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Container(
                constraints: BoxConstraints(maxWidth: 320.px),
                child: Text(
                  '${item.rewardDesc}',
                  style: TextStyle(
                    color: AppColors.fontColorGray99,
                    fontSize: 12.px,
                    overflow: TextOverflow.ellipsis,
                  ),
                  textAlign: TextAlign.left,
                ),
              ),
            ],
          ),
          hiSpace(height: 10.px),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                width: 140.px,
                child: RichText(
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  text: TextSpan(
                    children: <TextSpan>[
                      TextSpan(
                        text: '${item.nickName}',
                        style: TextStyle(
                          color: AppColors.fontColor,
                          fontSize: 12.px,
                        ),
                      ),
                      TextSpan(
                        text:
                            ' (${item.userTel.substring(item.userTel.length - 4)})',
                        style: TextStyle(
                          color: AppColors.fontColorGray99,
                          fontSize: 12.px,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              SizedBox(
                width: 150.px,
                child: Text(
                  '${item.rewardAmount}',
                  style: TextStyle(
                    color: AppColors.fontColor,
                    fontSize: 14.px,
                    overflow: TextOverflow.ellipsis,
                  ),
                  textAlign: TextAlign.right,
                ),
              ),
            ],
          ),
          hiSpace(height: 6.px),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            mainAxisSize: MainAxisSize.max,
            children: [
              SizedBox(
                width: 150.px,
                child: Text(
                  'ID: ${item.showId}',
                  style: TextStyle(
                    color: AppColors.fontColorGray99,
                    fontSize: 12.px,
                    overflow: TextOverflow.ellipsis,
                  ),
                  textAlign: TextAlign.left,
                ),
              ),
              Text(
                '${item.createTime}',
                style: TextStyle(
                  color: AppColors.fontColorGray99,
                  fontSize: 12.px,
                  overflow: TextOverflow.ellipsis,
                ),
                textAlign: TextAlign.right,
              ),
            ],
          ),
        ],
      ),
    );
  }
}
